/**
 * Created by lisiqin on 2016/2/14.
 */
/* -------------需要积分进行兑换-------------------------- */
/*convert是店家优惠券兑换，convert_2是个人发布优惠券兑换*/
function convert(credit,coupon_id,LoginPage){
    stopEventBubble();
    var token = getCookie("token");  //判断登录
    if(token==null||token==undefined||token==-1){
        LoginPage();
    }else{
        var url = "/Coupon/Receive";
        var arg = {};
        arg.id =  coupon_id;
        ajaxJsonp(url, arg,successful,"get",failMessage);
    }
    function successful(){
        var msg = '<div>'+
            '<span> 是否使用</span>'+
            '<span>'+credit+'</span>'+
            '<span>积分兑换该优惠券？</span>'+
            '<div>';
        success(msg);
    }
}
function convert_2(target,LoginPage){
    stopEventBubble();
    var token = getCookie("token");  //判断登录
    if(token==null||token==undefined||token==-1){
        LoginPage();
    }else{
        var url = "/Coupon/receiveFromUser";
        var arg = {};
        arg.coupon_code = $(target).attr("data-id");
        ajaxJsonp(url, arg,successful_2,"get",failMessage);
    }
    function successful_2(){
        var msg = '<div>'+
            '<span> 是否使用</span>'+
            '<span>'+$(target).attr("class")+'</span>'+
            '<span>积分兑换该优惠券？</span>'+
            '<div>';
        success(msg);
    }
}

/*可以兑换*/
function success(msg){
    $('#myModal .modal-body').text(" ");
    $('#myModal .modal-body').append(msg);
    $("#myModal .modal-footer button:nth-of-type(1)").text("返回");
    $("#myModal .modal-footer button:nth-of-type(2)").text("是");
    $('#myModal').modal();
    $("#myModal .modal-footer button:nth-of-type(2)").click(function(){
        $('#myModal').modal("hide");
        setTimeout(function(){
            $('#myModal .modal-body').text("兑换成功，分享信息可获得额外的积分");
            $("#myModal .modal-footer button:nth-of-type(1)").text("返回");
            $("#myModal .modal-footer button:nth-of-type(2)").text("分享");
            $('#myModal').modal();
            $("#myModal .modal-footer button:nth-of-type(2)").click(function(){
                window.location.href = "http://www.baidu.com";
            })
        },1000)
        $(".oneMall_jifen span").css({"display":"none"})
        $(".jifen_use").css({"display":"block"})
    })
}
/* -------------不需要积分直接领取-------------------------- */
function receive(target,LoginPage){
    stopEventBubble();
    var token = getCookie("token");
    if(token==null||token==undefined||token==-1){
        LoginPage();
    }else{
        var url = "/Coupon/isReceive";
        var arg = {};
        arg.id =  $(target).attr("data-id");
        ajaxJsonp(url, arg,success,"get",failMessage);
    }
    function success(res){
        for(var i=0;i<res.data.length;i++){
            var id=res.data[i].id;
            var is_receive=res.data[i].is_receive;
            var for_count =$(target).prev().find(".numberOne").text();
            console.log(for_count);
            if(is_receive==1){
                var url = "/Coupon/Receive";
                var arg = {};
                arg.id =  id;
                ajaxJsonp(url, arg,successful,"get",failMessage);
            }else{
                $('#Modal .modal-body').text("您的领取数额已经用完，不可再领取！");
                $("#Modal .modal-footer button").css({"margin-left":"20%"});
                $('#Modal').modal();
            }
            function successful(){
                $('#Modal .modal-body').text("领取成功！");
                $("#Modal .modal-footer button").css({"margin-left":"20%"});
                $('#Modal').modal();
                var count = for_count-1;
                console.log(count);
                $(target).parent().find(".number span:nth-of-type(1)").text(" ");
                $(target).parent().find(".number span:nth-of-type(1)").text(count);
            }
        }
    }
}

/*失败提示*/
function failMessage(res){
    $('#Modal .modal-body').text(res.status.msg);
    $("#Modal button").text("返回");
    $("#Modal button").css({"margin-left":"20%"});
    $('#Modal').modal();
}